<?php
require_once(__DIR__ . "/C5Request.php");
require_once(__DIR__ . "/C5Sessions.php");
require_once(__DIR__ . "/HttpClient.php");
require_once(__DIR__ . "/C5AuthUtils.php");

class C5Client extends HttpClient
{
    protected $remote_user_id = 'demo_user';
    protected $salt = 'salt123';
    protected $session_args = [];
    protected $auth_headers = [];

    public function __construct($remote_user_id, $salt)
    {
        $this->remote_user_id = $remote_user_id;
        $this->salt = $salt;
    }

    public function call($method = "POST")
    {
        $this->putSessionArgs();
        $this->md5Encrypt();
        parent::call($method);
    }

    public function putSessionArgs()
    {
        if (empty($this->session_args)) {
            return;
        }
        foreach ($this->session_args as $name) {
            $value = C5Sessions::get_session($name, "");
            $this->add_parameter($name, $value);
        }
    }

    public function md5Encrypt()
    {
//        $this->auth_headers["c5_auth_version"] = "2.0";
//        $this->auth_headers["remote_user_id"] = $this->remote_user_id;
//        $this->auth_headers["salt"] = $this->salt;
//        $this->auth_headers["timestamp"] = time() * 1000;
//        $this->auth_headers["random_string"] = rand(10000000,99999999);
//        $this->auth_headers["remote_verify_code"] = C5AuthUtils::encrypt($this->auth_headers, $this->remote_user_id, $this->salt);
//        unset($this->auth_headers["salt"]);
//        $this->add_headers($this->auth_headers);


        $this->add_parameter("remote_user_id", $this->remote_user_id);
        $this->add_parameter("timestamp", time() * 1000);
        $code = C5AuthUtils::encrypt($this->parameters, $this->remote_user_id, $this->salt, 0);
        $this->add_parameter("remote_verify_code", $code);
        if (array_key_exists("salt", $this->parameters)) {
            unset($this->parameters["salt"]);
        }
    }

    public function unsetArrayParameters()
    {
        foreach ($this->getParameters() as $key => $value) {
            if (is_array($value)) {
                $this->addParameter($key, "");
            }
        }
    }

    /**
     * @return string
     */
    public function getRemoteUserId()
    {
        return $this->remote_user_id;
    }

    /**
     * @param string $remote_user_id
     */
    public function setRemoteUserId($remote_user_id)
    {
        $this->remote_user_id = $remote_user_id;
    }

    /**
     * @return string
     */
    public function getSalt()
    {
        return $this->salt;
    }

    /**
     * @param string $salt
     */
    public function setSalt($salt)
    {
        $this->salt = $salt;
    }

    /**
     * @return array
     */
    public function getSessionArgs()
    {
        return $this->session_args;
    }

    /**
     * @param array $session_args
     */
    public function setSessionArgs($session_args)
    {
        $this->session_args = $session_args;
    }

    public function addSessionArgs($names)
    {
        if (is_string($names)) {
            $names = [$names];
        }

        foreach ($names as $name) {
            if (in_array($name, $this->session_args)) {
                continue;
            }
            array_push($this->session_args, $nmae);
        }

    }

}
